/**	
 * <br>
 * Copyright 2012 Chuan. All rights reserved.<br>
 * <br>			 
 * Package: com.chuan.mantu <br>
 * FileName: UploadActivity.java <br>
 * <br>
 * @version
 * @author javacyc@qq.com
 * @created 2012-11-15
 * @last Modified 
 * @history
 */

package com.chuan.mantu;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.chuan.mantu.extend.HttpMultipartPostAsyncTask;
import com.chuan.mantu.extend.InfoTipAsyncTask;
import com.chuan.mantu.utils.Const;
import com.chuan.mantu.utils.ViewUtil;

/**
 * {上传图片界面}
 * 
 * @author javacyc@qq.com
 * @created 2012-11-15 下午03:48:11
 * @lastModified
 * @history
 */

public class UploadActivity extends Activity {

	private String uploadFilePath;
	private String selectedItem;
	
	private ImageView imageView;
	private RadioButton radioButtonBrowse;
	private RadioButton radioButtonUpload;
	private RadioButton radionButtonSelect;
	private Context context;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.upload);
		context = this;
		imageView = (ImageView) findViewById(R.id.upload_imageview_info);
		radioButtonBrowse = (RadioButton) findViewById(R.id.upload_radionbutton_browse);
		radioButtonUpload = (RadioButton) findViewById(R.id.upload_radionbutton_upload);
		radionButtonSelect = (RadioButton) findViewById(R.id.upload_radionbutton_select);
		int width =	(((WindowManager)this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth() / 3);
		radioButtonBrowse.setWidth(width);
		radioButtonUpload.setWidth(width);
		radionButtonSelect.setWidth(width);
		radioButtonBrowse.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent=new Intent(); 
			    intent.setType(Const.IMAGE_TYPE); 
			    intent.setAction(Intent.ACTION_GET_CONTENT);//打开图像库 
			    startActivityForResult(intent, 1); 
			}
		});
		
		radioButtonUpload.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(uploadFilePath == null || uploadFilePath.equals("")){
					InfoTipAsyncTask it = new InfoTipAsyncTask(context, R.string.upload_infotip_choose);
					it.execute("");
					return;
				}
				
				if(selectedItem == null || selectedItem.equals("")){
					InfoTipAsyncTask it = new InfoTipAsyncTask(context, R.string.upload_infotip_sort);
					it.execute("");
					return;
				}
				
				new AlertDialog.Builder(UploadActivity.this)
				.setTitle(getString(R.string.button_confirm))
				.setMessage(getString(R.string.upload_activity_1))
				.setPositiveButton(getString(R.string.button_confirm), new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						HttpMultipartPostAsyncTask httpMultipartPostAsyncTask = new HttpMultipartPostAsyncTask(UploadActivity.this, uploadFilePath, selectedItem);
						httpMultipartPostAsyncTask.execute("");
					}
				})
				.setNegativeButton(getString(R.string.button_cancel), null)
				.show();
			}
		});
		
		radionButtonSelect.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				final String[] items = new String[]{getString(R.string.title_pic_category_yinhun)
						,getString(R.string.title_pic_category_jingdian)
						,getString(R.string.title_pic_category_gaoxiao)
						,getString(R.string.title_pic_category_gangman)
						,getString(R.string.title_pic_category_mairou)
						,getString(R.string.title_pic_category_qita)};
				
				new AlertDialog.Builder(UploadActivity.this)
					.setTitle(getString(R.string.upload_activity_2))
					.setIcon(android.R.drawable.ic_dialog_info)
					.setSingleChoiceItems(items,0,new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							radionButtonSelect.setText(items[which]);
							selectedItem = getResources().getStringArray(R.array.category_filename)[which];
							dialog.dismiss();
						}
					})
					.setNegativeButton(getString(R.string.button_cancel), null)
					.show();
			}
		});
		
		
		InfoTipAsyncTask it = new InfoTipAsyncTask(context, R.string.upload_activity_3);
		it.execute("");
	}

	/**
	 *  调用图库后，返回的结果
	 *  @param requestCode
	 *  @param resultCode
	 *  @param data
	 *  @author javacyc@qq.com
	 *  @created 2012-11-27 下午01:30:13
	 *  @lastModified      
	 *  @history
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == RESULT_OK){
			Uri uri = data.getData();
				String[] proj={MediaStore.Images.Media.DATA};
				Cursor cursor=managedQuery(uri, proj, null, null, null); 
				int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
				cursor.moveToFirst();//小心游标越界，要将其移动到前段
				uploadFilePath = cursor.getString(column_index);
				if((new File(uploadFilePath)).length() > 512000){ //图片大小限定为最大512KB
					InfoTipAsyncTask it = new InfoTipAsyncTask(context, R.string.upload_infotip_size);
					it.execute("");
					uploadFilePath = "";
				}else{
					try {
						imageView.setImageBitmap(ViewUtil.addBitmapShrink(uploadFilePath));
					} catch (OutOfMemoryError e){
						e.printStackTrace();
					}
				}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	public void uploadSuccess(){
		this.uploadFilePath = "";
		this.selectedItem = "";
		InfoTipAsyncTask it = new InfoTipAsyncTask(context, R.string.upload_infotip_success);
		it.execute("");
	}
	
}